package com.SeleniumTestsGoogleChrome;

import com.thoughtworks.selenium.Selenium;
import com.thoughtworks.selenium.SeleneseTestBase;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.util.regex.Pattern;
import static org.junit.Assert.*;

public class SearchFlightTestCases extends SeleneseTestBase {

	@Before
	public void setUp() throws Exception {
		File file = new File(
				"C:/Users/Steven/Documents/4th Year/EECE 419/chromedriver.exe");
		System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
		WebDriver driver = new ChromeDriver();
		String baseUrl = "http://localhost:8080/Website/";
		selenium = new WebDriverBackedSelenium(driver, baseUrl);
	}

	// Created by Steven Chow
	// Scenario 1: Avaliable Flights Displayed
	// Test Case 1: Search without using filter
	@Test
	public void testSearchFlightSC1TC1() throws Exception {
		// Go to website
		selenium.open("");
		// Check if there is a search flight button
		assertTrue(selenium.isElementPresent("css=div.tile-content.icon"));
		// Click Seach Flight Button
		selenium.click("css=div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// Check if we are on the right page
		assertEquals("Search Flight", selenium.getTitle());
		// Check if there is a buttong called "Search"
		assertTrue(selenium.isTextPresent("Search"));
		// Search without using the filter
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we have all the flights in the Database displayed
		assertEquals(
				"1000 2012-10-09 13:00 Air Canada Vancouver Los Angeles 200.0 122",
				selenium.getText("//html/body/div/div[2]/div/div/div/div/div[2]/form/table/tbody/tr"));
		assertEquals(
				"1001 2012-10-10 14:00 Air Canada Los Angeles Vancouver 200.0 122",
				selenium.getText("//html/body/div/div[2]/div/div/div/div/div[2]/form/table/tbody/tr[2]"));
	}

	// Created by Steven Chow
	// Scenario 1: Avaliable Flights Displayed
	// Test Case 2: Search using filter
	@Test
	public void testSearchFlightSC1TC2() throws Exception {
		// Go to website
		selenium.open("");
		// Check if there is a search flight button
		assertTrue(selenium.isElementPresent("css=div.tile-content.icon"));
		// Click Seach Flight Button
		selenium.click("css=div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// Check if we are on the right page
		assertEquals("Search Flight", selenium.getTitle());
		// Check if there is a buttong called "Search"
		assertTrue(selenium.isTextPresent("Search"));
		// Search Filter using "Leaving from:"
		selenium.type("name=deploc", "Vancouver");
		// Click Search
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we get something
		assertEquals(
				"1000 2012-10-09 13:00 Air Canada Vancouver Los Angeles 200.0 122",
				selenium.getText("//html/body/div/div[2]/div/div/div/div/div[2]/form/table/tbody/tr"));
		// Repeat for "Departuing Date"
		selenium.type("name=depdat", "1010");
		// Click Search
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we get something
		assertEquals(
				"1001 2012-10-10 14:00 Air Canada Los Angeles Vancouver 200.0 122",
				selenium.getText("//html/body/div/div[2]/div/div/div/div/div[2]/form/table/tbody/tr"));
		// Repeat for "Going to"
		selenium.type("name=arriloc", "Los Angeles");
		// Click Search
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we get something
		assertEquals(
				"1000 2012-10-09 13:00 Air Canada Vancouver Los Angeles 200.0 122",
				selenium.getText("//html/body/div/div[2]/div/div/div/div/div[2]/form/table/tbody/tr"));
		// Repeat for "Returning date"
		selenium.type("name=arridat", "1010");
		// Click Search
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we get something
		assertEquals(
				"1001 2012-10-10 14:00 Air Canada Los Angeles Vancouver 200.0 122",
				selenium.getText("//html/body/div/div[2]/div/div/div/div/div[2]/form/table/tbody/tr"));
		// Try filling in everything
		selenium.type("name=deploc", "Vancouver");
		selenium.type("name=depdat", "1009");
		selenium.type("name=arriloc", "Los Angeles");
		selenium.type("name=arridat", "1010");
		// Click Search
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we get something
		assertEquals(
				"1000 2012-10-09 13:00 Air Canada Vancouver Los Angeles 200.0 122",
				selenium.getText("//html/body/div/div[2]/div/div/div/div/div[2]/form/table/tbody/tr"));
	}

	// Created by Steven Chow
	// Scenario 2: Avaliable Flights not Displayed
	// Test Case 2: Invalid Dates
	@Test
	public void testSearchFlightSC2TC1() throws Exception {
		// Go to website
		selenium.open("");
		// Check if there is a search flight button
		assertTrue(selenium.isElementPresent("css=div.tile-content.icon"));
		// Click Seach Flight Button
		selenium.click("css=div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// Check if we are on the right page
		assertEquals("Search Flight", selenium.getTitle());
		// Check if there is a buttong called "Search"
		assertTrue(selenium.isTextPresent("Search"));
		// Add an invalid Date
		selenium.type("name=depdat", "add");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we get no results
		assertTrue(selenium.isTextPresent("No Flights are found"));
		selenium.type("name=arridat", "lalala");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we get no results
		assertTrue(selenium.isTextPresent("No Flights are found"));
	}

	// Created by Steven Chow
	// Scenario 2: Avaliable Flights not Displayed
	// Test Case 2: There are no flights added
	@Test
	public void testSearchFlightSC2TC2() throws Exception {
		// Go to website
		selenium.open("");
		// Check if there is a search flight button
		assertTrue(selenium.isElementPresent("css=div.tile-content.icon"));
		// Click Seach Flight Button
		selenium.click("css=div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// Check if we are on the right page
		assertEquals("Search Flight", selenium.getTitle());
		// Check if there is a buttong called "Search"
		assertTrue(selenium.isTextPresent("Search"));
		// Enter a Departure location that does not exist
		selenium.type("name=deploc", "12");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we don't get anything
		assertEquals("", selenium.getText("No Flights are found"));
		// Enter a Departure date that does not exist
		selenium.type("name=depdat", "1103");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we don't get anything
		assertEquals("", selenium.getText("No Flights are found"));
		// Enter a Arrival location that does not exist
		selenium.type("name=arriloc", "12");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we don't get anything
		// Enter a Arrival Date that does not exist
		selenium.type("name=arridat", "1015");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we don't get anything
		assertEquals("", selenium.getText("No Flights are found"));
		// Enter everything that does not exist
		selenium.type("name=deploc", "skajlh");
		selenium.type("name=arriloc", ";lsjflkgsjfkljgsdf");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Check if we don't get anything
		assertEquals("", selenium.getText("No Flights are found"));
	}

	// Created by Steven Chow
	// Scenario 2: Avaliable Flights not Displayed
	// Test Case 3: Search flight cancelled

	@Test
	public void testSearchFlightSC2TC3() throws Exception {
		// Go to website
		selenium.open("");
		// Check if there is a search flight button
		assertTrue(selenium.isElementPresent("css=div.tile-content.icon"));
		// Click Seach Flight Button
		selenium.click("css=div.tile-content.icon");
		selenium.waitForPageToLoad("30000");
		// Check if we are on the right page
		assertEquals("Search Flight", selenium.getTitle());
		// Check if there is a buttong called "Search"
		assertTrue(selenium.isTextPresent("Search"));
		// Search for crap
		selenium.type("name=deploc", "12");
		selenium.type("name=arriloc", "12");
		selenium.click("css=input[type=\"submit\"]");
		selenium.waitForPageToLoad("30000");
		// Go back
		selenium.click("//a[contains(@href, '/Website/index_loggedin.jsp?username=null')]");
		selenium.waitForPageToLoad("30000");
		// Check if we are on the main page
		assertEquals("PlaneSimple", selenium.getTitle());
	}

	@After
	public void tearDown() throws Exception {
		selenium.stop();
	}

}
